jclass={
    initPage:function(){
        // 绑定dom对象
        this.obj = {
            searchText:$('#searchText'),
        };
        // 声明全局变量
        this.data = {
            datatable:[],
            tableParams: {
                page:1,
                size:10
            },
            addDialogParams: {
                ruleName: '',
                alarmLevel: '2',
                supervisoryLevel: '1',
                agingTime: '',
                supervisoryDeadline: ''
            },
            selectdbLevelArr:[{val:'1',name:'紧急'},{val:'2',name:'一般'}],
        };
        this.getTableList();
        // this.creatTable({data:{},total:0});
        this.init();
        this.bindEvent();
    },
    init() {
        let that = this

        // 重置
        $("#resetClick").click(() => {
            this.data.tableParams = {
                page:1,
                size:10
            }
        })

        // 查询
        $("#searchClick").click(() => {
            this.getTableList()
        })

        jdSelect.JdSelectRadioInit('#dselect_dblevel',this.data.selectdbLevelArr,(select) => {
             this.data.addDialogParams.supervisoryLevel = select.val
        })

    },
    bindEvent() {

        // $("#d_db_select").change(() => {
        //     this.data.addDialogParams.supervisoryLevel = $("#d_db_select option:selected").val()
        // })
        $("#add_btn_submit").click(() => {
            let reg = /^[1-9]\d*$/
            if (!reg.test($("#d_input_timejg").val()) || !reg.test($("#d_input_timejz").val())) {
                JPop.alert({msg:'时间必须大于0',type:'error'})
                return false
            }
            let dataform = $("#form_dbadd").serializeArray()
            let a = true
            console.log(dataform)
            dataform.forEach(item => {
                if(!item.value) a=false
            })
            if(a) {
                this.data.addDialogParams.ruleName = $("#d_input_rulename").val()
                this.data.addDialogParams.agingTime = $("#d_input_timejg").val()
                this.data.addDialogParams.supervisoryDeadline = $("#d_input_timejz").val()
                if($("#jdialog_title").text() === '编辑') {
                    jrequest.put("/api/automatic-supervision",this.data.addDialogParams).then(res => {
                        if (res) {
                            JPop.alert({msg:'修改成功！',type:'success'})
                            closeDialog()
                            this.getTableList()
                        }
                    })
                } else {
                    this.data.addDialogParams.id = ''
                    jrequest.post("/api/automatic-supervision",this.data.addDialogParams).then(res => {
                        if (res) {
                            JPop.alert({msg:'添加成功！',type:'success'})
                            closeDialog()
                            this.getTableList()
                        }
                        
                    })
                }
                return false
            }
            
        })
    },
    getTableList() {
        jrequest.get("/api/automatic-supervision",this.data.tableParams).then(res => {
            res.data.forEach(item => {
                item.isSwitchName = item.isSwitch == 0 ? '开启' : '关闭'
            })
            this.data.datatable = res.data
            $("#jtable_autodb").bootstrapTable('destroy');
            this.creatTable(res)
            // $("#jtable_autodb").bootstrapTable('load',res.data);
        })
    },
    creatTable:function(res){
        var self=this;
        var columnsOne = [
            {
                title : '序号',
                align: "center",
                formatter: function (value, row, index) {
                    //获取每页显示的数量
                    var pageSize=self.data.tableParams.size;
                    //获取当前是第几页
                    var pageNumber=self.data.tableParams.page;
                    //返回序号，注意index是从0开始的，所以要加上1
                    return pageSize * (pageNumber - 1) + index + 1;
                }
            },
            {
              field: "ruleName",
              title: "规则名称"
            },
            {
              field: "alarmLevel",
              title: "报警等级"
            },
            {
              field: "platformName",
              title: "平台"
            },
            {
              field: "owersName",
              title: "企业"
            },
            {
              field: "agingTime",
              title: "督办间隔时间"
            },
            {
              field: "supervisoryLevel",
              title: "督办等级"
            },
            {
              field: "supervisoryDeadline",
              title: "督办截止时间"
            },
            {
              field: "isSwitchName",
              title: "督办状态"
            },
            {
              field: "founder",
              title: "创建人",
              visible: false
            },
            {
              field: "founderTel",
              title: "创建人电话",
              visible: false
            },
            {
              field: "founderEmail",
              title: "创建人邮箱",
              visible: false
            },
            {
              field: "createTime",
              title: "创建时间",
              visible: false
            },
            {
              field: "supervisor",
              title: "取消督办人",
              visible: false
            },
            {
              field: "supervisorTel",
              title: "取消督办人电话",
              visible: false
            },
            {
              field: "supervisorEmail",
              title: "取消督办人邮箱",
              visible: false
            },
            {
              field: "cancellationTime",
              title: "取消时间",
              visible: false
            },
            {
                field: 'Button',
                title: '操作',
                width: "200",
                align: 'center',
                events: operateEvents,
            }
        ];
        self.tableEvent("jtable_autodb",res.data,columnsOne,true,res.total);
    },
    tableEvent:function(id,data,columns,paginationShow,total){
        data.forEach(item => {
            item.Button = this.operateFormatter(item.isSwitch)
        })
        let that = this
        $('#' + id).bootstrapTable({
            method: 'get',
            cache: false,
            height: 320,
            pagination: paginationShow,
            pageSize: that.data.tableParams.size,
            pageNumber:that.data.tableParams.page,
            striped: true,
            columns: columns,
            sidePagination: 'server', // client or server
            totalRows: total,
            data :data,
            onPageChange: function (size, number) {
                console.log(number)
                that.data.tableParams.page = number
                that.data.tableParams.size = size
                that.getTableList()
            },
            formatNoMatches: function(){
                return '暂无数据';
            }
        });
    },
    operateFormatter(isSwitch) {
        if (isSwitch == 0) { // 开启状态
            return [
                '<button type="button" id="table_edit">修改</button>',
                '<button type="button" id="table_close">关闭</button>'
            ].join('');
        } else { // 关闭状态
            return [
                '<button type="button" id="table_edit">修改</button>',
                '<button type="button" id="table_close">开启</button>'
            ].join('');
        }
        
    }
    
}

$(function(){
    jclass.initPage();

    // 表格操作按钮
    window.operateEvents = {
        'click #table_edit': (e,value,row,index) => {
            // console.log(row)
            openDialog("edit",row.supervisoryLevel)
            $("#d_input_rulename").val(row.ruleName)
            $("#d_input_timejz").val(row.supervisoryDeadline)
            $("#d_input_timejg").val(row.agingTime)

            jclass.data.addDialogParams.supervisoryLevel = row.supervisoryLevel
            jclass.data.addDialogParams.id = row.id
        },
        'click #table_close': (e,value,row,index) => {
            // console.log(row)
            let msg = ''
            if (row.isSwitch == 0) {
                msg = "确定要关闭督办？"
            } else {
                msg = "确定要开启督办？"
            }
            // Jdialog.confirm({ message: msg }).on(function (e) {
            //     if (e) {
            //         console.log(row.id)
            //         let isSwitch = 0
            //         if (row.isSwitch == 0) {
            //             isSwitch = 1
            //         } else {
            //             isSwitch = 0
            //         }
            //         jrequest.put(`/api/automatic-supervision`,{id:row.id,isSwitch:isSwitch}).then(res => {
            //             if (res) {
            //                 jclass.getTableList()
            //             }
            //         })
            //     }
            // });
            JPop.dialogDel({msg:msg,operate:true}).on(success => {
                if (success) {
                    let isSwitch = 0
                    if (row.isSwitch == 0) {
                        isSwitch = 1
                    } else {
                        isSwitch = 0
                    }
                    jrequest.put(`/api/automatic-supervision`,{id:row.id,isSwitch:isSwitch}).then(res => {
                        if (res) {
                            jclass.getTableList()
                        }
                    })
                }
            })
        }
    }
});

function timeBlurHandler(id) {
    let val = $(id).val()
    let reg = /^[1-9]\d*$/
    if (!reg.test(val)) {
        JPop.alert({msg:'时间必须大于0',type:'error'})
    }
}

function openDialog(type,dbLevel) {
    type = type || ''
    dbLevel = dbLevel || ''
    if (type === 'edit') {
        $("#jdialog_title").text("编辑")
        // $("#d_db_select").find(`option[value=${dbLevel}]`).attr("selected",true)
        jdSelect.setSelectRadio("#dselect_dblevel",jclass.data.selectdbLevelArr,dbLevel)
    } else {
        $("#jdialog_title").text("添加")
    }
    $("#autodbDialog").show()
}
function closeDialog() {
    $("#d_input_rulename").val('')
    $("#d_input_timejz").val('')
    $("#d_input_timejg").val('')
    // $("#d_db_select").prop("selectedIndex", 0)
    jdSelect.resetSelectRadio("#dselect_dblevel",jclass.data.selectdbLevelArr[0].name)

    jclass.data.addDialogParams = {
        ruleName: '',
        alarmLevel: '2',
        supervisoryLevel: '1',
        agingTime: '',
        supervisoryDeadline: ''
    }

    $("#autodbDialog").hide()
}


